package com.xln.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.time.LocalDateTime;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_article")
public class Article {
    @TableId(value = "id",type = IdType.AUTO)
    private Long id;

    private String title;

    private String content;

    private String summary;

    private String coverImage;

    private Long authorId;

    private Long categoryId;

    private Integer status = 0; // 0-草稿, 1-已发布, 2-审核中, 3-已下线

    private String flag; // 文章标记：如"原创"、"转载"、"翻译"、"热门"等

    private Integer isTop = 0; // 0-否, 1-是

    private Integer isRecommend = 0; // 0-否, 1-是

    private Integer isComment = 1; // 0-否, 1-是

    private Integer viewCount = 0;

    private Integer likeCount = 0;

    private Integer collections = 0; // 收藏数

    private Integer commentCount = 0;

    private String slug;

    private String tags;

    private String metaKeywords;

    private String metaDescription;

    @TableField(exist = false)
    private String categoryName;

    @TableField(value = "publish_time", typeHandler = LocalDateTimeTypeHandler.class)
    private LocalDateTime publishTime;
    @TableField(value = "create_time", typeHandler = LocalDateTimeTypeHandler.class)
    private LocalDateTime createTime;
    @TableField(value = "update_time", typeHandler = LocalDateTimeTypeHandler.class)
    private LocalDateTime updateTime;

    // 文章状态枚举
    public enum ArticleStatus {
        DRAFT(0, "草稿"),
        PUBLISHED(1, "已发布"),
        UNDER_REVIEW(2, "审核中"),
        OFFLINE(3, "已下线");

        private final int code;
        private final String description;

        ArticleStatus(int code, String description) {
            this.code = code;
            this.description = description;
        }

        public int getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        // 根据code值获取该状态文章的枚举值 如：DRAFT(0, "草稿")
        public static ArticleStatus getByCode(int code) {
            for (ArticleStatus status : values()) {
                if (status.code == code) {
                    return status;
                }
            }
            return DRAFT;
        }
    }

    // 判断文章是否已发布  是否草稿
    public boolean isPublished() {
        return this.status == ArticleStatus.PUBLISHED.getCode();
    }

    // 增加收藏数
    public void incrementCollections() {
        this.collections++;
    }

    // 减少收藏数
    public void decrementCollections() {
        if (this.collections > 0) {
            this.collections--;
        }
    }
}
